package com.metagurukul.metagranth.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import com.metagurukul.metagranth.exception.MetaSqlException;
import com.metagurukul.metagranth.model.common.connection.ConnectionPoolFactory;
import com.metagurukul.metagranth.model.common.dbutil.Util;
import com.metagurukul.metagranth.vo.QuestionTagMapping;
/**
 * 
 * @author team metaGranth
 * this class will perform all the operation related to the Question-tag table
 *
 */
public class QuestionTagMappingDAO {
	
	/**
	 * 
	 * @param questionTagMapping stores the value object of the QuestionTagMapping class
	 * @return	status whether the question tag mapping has been saved or not in the table
	 * @throws MetaSqlException
	 */
	public static int save(QuestionTagMapping questionTagMapping) throws MetaSqlException{

		int status = 0;
		//creating and retrieve the connection from the connectionPoolFactory
		ConnectionPoolFactory cp = ConnectionPoolFactory.getInstance();
		Connection con = cp.checkOut();
		
		int tagId = questionTagMapping.getTagId();
		int questionID = questionTagMapping.getQuestionId();
		String query = "Insert into METACUBEGRANTH_QUESTION_TAG(TAG_IDENTIFICATION_NUMBER,QUESTION_IDENTIFICATION_NUMBER) values(?,?)";
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement(query);
			//set the values in the prepared statement
			ps.setInt(1, tagId);
			ps.setInt(2, questionID);
			
			status = ps.executeUpdate();
		} catch (SQLException e) {
			//if there is any sql exception  then the exception is thrown
			throw new MetaSqlException(e);
		} finally {
			//close all the open database connection
			Util.closePreparedStatement(ps);
			//checkIn the connection again to the ConnectionPoolFactory
			 cp.checkIn(con);
		}
		return status;
	}
	
	/**
	 * 
	 * @param questionID stores the question_identification_number which is used to delete the mapping from the QUESTION_TAG table
	 * @throws MetaSqlException 
	 * 
	 */
	public static void deleteRecordsByQid(int questionID) throws MetaSqlException{
		//create and retrieve the connection from the connection pool factory
		ConnectionPoolFactory cp = ConnectionPoolFactory.getInstance();
		Connection con = cp.checkOut();
		
		String queryToDelete = "delete from METACUBEGRANTH_QUESTION_TAG where QUESTION_IDENTIFICATION_NUMBER = "+questionID ;
		
		Statement stmt = null;
		
		// deleting exsting records first
		try {
			stmt = con.createStatement();
			stmt.executeUpdate(queryToDelete);
		} catch (SQLException e) {			
			//if there is any sql exception  then the exception is thrown
			throw new MetaSqlException(e);
		}finally {
			//close all the open database connection
			Util.closeStatement(stmt);
			//checkIn the connection again to the ConnectionPoolFactory
			 cp.checkIn(con);
		}
		
	}
	
	/**
	 * 
	 * @param questionTagMapping is the object of the QuestionTagMapping class which is to be stored in the database
	 * @throws MetaSqlException 
	 */
	public static void updateQuestionTagMapping(QuestionTagMapping questionTagMapping) throws MetaSqlException {
		//create and retrieve the connection from the connection pool factory
		ConnectionPoolFactory cp = ConnectionPoolFactory.getInstance();
		Connection con = cp.checkOut();
		
		int tagId = questionTagMapping.getTagId();
		int questionID = questionTagMapping.getQuestionId();
		
		String query = "Insert into METACUBEGRANTH_QUESTION_TAG(TAG_IDENTIFICATION_NUMBER,QUESTION_IDENTIFICATION_NUMBER) values(?,?)";
		PreparedStatement ps = null;		
		
		try {	
			
			// inserting new records
			ps = con.prepareStatement(query);
			ps.setInt(1, tagId);
			ps.setInt(2, questionID);
			
			ps.execute();
			
		} catch (SQLException e) {
			//if there is any sql exception  then the exception is thrown
			throw new MetaSqlException(e);
		} finally {
			//close all the open database connection
			Util.closeStatement(ps);
			//checkIn the connection again to the ConnectionPoolFactory
			 cp.checkIn(con);
		}
		
	}
}
